/**
 *
 */
package org.whuims.pois.hibernate;

import com.mysql.jdbc.Messages;
import org.hibernate.*;
import org.hibernate.criterion.Restrictions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @author zhenjw
 */
public class BaseDao implements IBaseDao {
    // private Messages Messages=new Messages();  
    private String databaseError = Messages
            .getString("BaseDao.databaseError");
    private SessionFactory sessionFactory;

    /*
     * 通过主键数组批量删除
     *(non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#batchDelete(java.lang.Class, long[]) 
     */
    public void batchDelete(Class clazz, long[] id) {
        // TODO Auto-generated method stub  
        String strId = "";
        for (int i = 0; i < id.length; i++) {
            if (i > 0)
                strId += ", " + id[i];
            else
                strId = "" + id[i];
        }

        for (int i = 0; i < id.length; i++) {
            this.delete(clazz, id[i]);
        }

    }

    /**
     * 创建对象
     *
     * @param entity
     */
    public void create(Object entity) {
        // TODO Auto-generated method stub  
        Session session = null;
        Transaction tr = null;
        boolean commitflag = false;
        try {
            session = this.getSession();
            tr = session.beginTransaction();
            session.save(entity);
            tr.commit();
            commitflag = true;
            session.flush();
            session.clear();
            // super.getHibernateTemplate().save(bean);

        } catch (Exception e) {

            String error = Messages.getString("BaseDao.create.saveError");

            e.printStackTrace();
            throw new DaoException(error);
        } finally {
            if (!commitflag) {
                try {
                    if (tr != null)
                        tr.rollback();
                } catch (Exception e1) {

                }
            }
            this.closeSession(session);
        }

    }

    /**
     * 更新对象，通过主键
     *
     * @param entity
     */
    @Override
    public void update(Object entity) {
        Session session = null;
        Transaction tr = null;
        boolean commitflag = false;
        try {
            session = this.getSession();
            tr = session.beginTransaction();
            session.update(entity);
            tr.commit();
            commitflag = true;
            session.flush();
            session.clear();
            // super.getHibernateTemplate().save(bean);

        } catch (Exception e) {

            String error = Messages.getString("BaseDao.update.updateError");

            e.printStackTrace();
            throw new DaoException(error);
        } finally {
            if (!commitflag) {
                try {
                    if (tr != null)
                        tr.rollback();
                } catch (Exception e1) {

                }
            }
            this.closeSession(session);
        }
    }

    /**
     * 删除对象，通过主键
     *
     * @param entity
     */
    public void delete(Object entity) {
        // TODO Auto-generated method stub  

        Session session = null;
        Transaction tr = null;
        boolean commitflag = false;
        try {
            session = this.getSession();
            tr = session.getTransaction();
            tr.begin();
            session.delete(entity);
            tr.commit();
            commitflag = true;
            session.flush();
            session.clear();
        } catch (Exception e) {
            if (tr != null)
                tr.rollback();
            String Error = Messages.getString("BaseDao.delete.Error");

            throw new DaoException(Error);
        } finally {
            if (!commitflag) {
                try {
                    if (tr != null)
                        tr.rollback();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            this.closeSession(session);
        }
    }


    /**
     * 删除对象，通过主键id
     *
     * @param clazz
     * @param id
     */
    public void delete(Class clazz, long id) {
        // TODO Auto-generated method stub  

        try {
            Object entity = this.getByPk(clazz, id);
            if (entity != null)
                this.delete(entity);

        } catch (Exception e) {

            throw new DaoException(Messages.getString("BaseDao.delete.Error"));
        }
    }

    /**
     * 删除所有
     *
     * @param clazz
     */
    public void deleteAll(Class clazz) {
        // TODO Auto-generated method stub  

        Session session = null;
        Transaction tr = null;
        boolean commitflag = false;
        try {
            session = this.getSession();
            tr = session.beginTransaction();
            Query query = session.createQuery(" delete   from "
                    + clazz.getName());
            query.executeUpdate();
            tr.commit();
            commitflag = true;
            session.flush();
        } catch (Exception e) {
            String Error = Messages.getString("BaseDao.delete.Error");

            throw new DaoException(Error);
        } finally {
            if (!commitflag) {
                try {
                    if (tr != null)
                        tr.rollback();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            this.closeSession(session);
        }
    }

    /**
     * 批量删除一个集合中的所有对象
     *
     * @param entities
     */
    public void deleteAll(Collection entities) {
        // TODO Auto-generated method stub  
        Session session = null;
        Transaction tr = null;
        boolean commitflag = false;
        try {
            session = this.getSession();
            tr = session.beginTransaction();
            Iterator ite = entities.iterator();

            while (ite.hasNext())
                session.delete(ite.next());
            // super.getHibernateTemplate().deleteAll(entities);  
            tr.commit();
            commitflag = true;
            session.flush();

        } catch (Exception e) {
            String error = Messages.getString("BaseDao.deleteAll.Error");

            throw new DaoException(error);
        } finally {
            if (!commitflag) {
                try {
                    if (tr != null)
                        tr.rollback();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            this.closeSession(session);
        }

    }

    /**
     * 执行sql语句
     *
     * @param sql
     */
    public void excuteSql(String sql) {
        // TODO Auto-generated method stub  

        Session session = null;
        Transaction tr = null;
        boolean commitflag = false;
        try {
            session = this.getSession();
            tr = session.beginTransaction();
            SQLQuery query = session.createSQLQuery(sql);

            query.executeUpdate();
            tr.commit();
            commitflag = true;
            session.flush();
            // super.getHibernateTemplate().update(bean);
        } catch (Exception e) {

            String Error = Messages.getString("BaseDao.excuteSql.Error");

            throw new DaoException(Error);
        } finally {
            if (!commitflag) {
                try {
                    if (tr != null)
                        tr.rollback();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            this.closeSession(session);
        }

    }

    public List<Object> queryWithResult(String sql) {
        // TODO Auto-generated method stub

        Session session = null;
        Transaction tr = null;
        boolean commitflag = false;
        List<Object> list;
        try {
            session = this.getSession();
            SQLQuery query = session.createSQLQuery(sql);
            list = query.list();
            commitflag = true;
            // super.getHibernateTemplate().update(bean);
        } catch (Exception e) {

            String Error = Messages.getString("BaseDao.excuteSql.Error");

            throw new DaoException(Error);
        } finally {
            if (!commitflag) {
                try {
                    if (tr != null)
                        tr.rollback();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            this.closeSession(session);
        }
        return list;
    }

    public List queryLast(int first, int last, String hql) {
        List result = null;
        Session session = null;

        try {
            session = this.getSession();
            Query e = session.createQuery(hql).setCacheable(true);
            if(first > 0 && last > 0) {
                e.setFirstResult(first);
                e.setMaxResults(last);
            }

            result = e.list();
        } catch (Exception var10) {
            throw new DaoException(this.databaseError);
        } finally {
            this.closeSession(session);
        }

        return result;
    }

    /**
     * 通过hql语句进行查找
     *
     * @param hql
     * @return
     */
    public List find(String hql) {
        // TODO Auto-generated method stub  
        List result = null;

        Session session = null;
        try {
            // result = super.getHibernateTemplate().find(queryString);  
            session = this.getSession();
            Query query = session.createQuery(hql);
            result = query.list();
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    public List findTopN(String hql, int n) {
        // TODO Auto-generated method stub
        List result = null;

        Session session = null;
        try {
            // result = super.getHibernateTemplate().find(queryString);
            session = this.getSession();
            Query query = session.createQuery(hql);
            query.setMaxResults(n);
            result = query.list();
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /**
     * 通过hql语句和参数数组进行查找
     *
     * @param hql
     * @param params
     * @return
     */
    public List findByParamsList(String hql, List params) {
        // TODO Auto-generated method stub

        List result = null;
        Session session = null;
        try {
            session = this.getSession();
            Query query = session.createQuery(hql);
//            query.setResultTransformer(RootEntityResultTransformer.INSTANCE);
            for (int i = 0; i < params.size(); i++) {
                query.setParameter(i, params.get(i));
            }
            result = query.list();
            // result = super.getHibernateTemplate().find(queryString, param);
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /**
     * 通过hql语句和参数数组进行查找
     *
     * @param hql
     * @param params
     * @return
     */
    public List findByParamsArr(String hql, Object... params) {
        // TODO Auto-generated method stub

        List result = null;
        Session session = null;
        try {
            session = this.getSession();
            Query query = session.createQuery(hql);
//            query.setResultTransformer(RootEntityResultTransformer.INSTANCE);
            for (int i = 0; i < params.length; i++) {
                query.setParameter(i, params[i]);   //TODO 坑爹，这里params忘记加[i]了，坑了我一整天啊
            }
            result = query.list();
            // result = super.getHibernateTemplate().find(queryString, param);
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }


    /**
     * 通过一个属性去查找
     *
     * @param hql
     * @param name
     * @param param
     * @return
     */
    public List findByNamedParam(String hql, String name, Object param) {
        // TODO Auto-generated method stub  

        List result = null;
        Session session = null;
        try {
            session = this.getSession();
            Query query = session.createQuery(hql);
            query.setParameter(name, param);
            result = query.list();
            // result =  
            // super.getHibernateTemplate().findByNamedParam(queryString,  
            // name, param);  
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#findBySql(java.lang.String) 
     */
    public List findBySql(String strsql) {
        // TODO Auto-generated method stub  

        System.out.println(strsql);
        Session session = null;
        List result = null;

        try {
            session = this.getSession();
            SQLQuery query = session.createSQLQuery(strsql);
            result = query.list();
            // super.getHibernateTemplate().update(bean);
        } catch (Exception e) {
            String Error = Messages.getString("BaseDao.exceuteSQL.Error");

            throw new DaoException(Error);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#findBySql(java.lang.String, java.util.List) 
     */
    public List findBySql(String strsql, List params) {
        // TODO Auto-generated method stub  
        String paramnameArray = "";
        if (params != null) {
            for (int i = 0; i < params.size(); i++) {
                if (i > 0)
                    paramnameArray += " , " + params.get(i);
                else
                    paramnameArray = "" + params.get(i);
            }
        }

        System.out.println(strsql);
        List result = null;
        Session session = null;
        try {
            session = this.getSession();

            SQLQuery query = session.createSQLQuery(strsql);
            if (params != null) {
                for (int i = 0; i < params.size(); i++) {
                    query.setParameter(i, params.get(i));
                }
            }
            result = query.list();
        } catch (Exception e) {
            String error = Messages.getString("BaseDao.findBySql.Error");

            e.printStackTrace();
            throw new DaoException(error);
        } finally {
            this.closeSession(session);
        }
        return result;
    }


    /**
     * 通过long类型主键得到一个对象
     *
     * @param clazz
     * @param id
     * @return
     */
    public Object getByPk(Class clazz, long id) {
        // TODO Auto-generated method stub  

        Object result = null;
        Session session = null;
        try {
            session = this.getSession();
            result = session.get(clazz, new Long(id));
            // (Object) super.getHibernateTemplate().get(clazz, id);  
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /**
     * 通过string类型主键得到一个对象
     *
     * @param clazz
     * @param id
     * @return
     */
    public Object getByPk(Class clazz, String id) {
        // TODO Auto-generated method stub

        Object result = null;
        Session session = null;
        try {
            session = this.getSession();
            result = session.get(clazz, id);
            // result = (Object) super.getHibernateTemplate().get(clazz, id);
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }
    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#getSession() 
     */
    public Session getSession() {
        // TODO Auto-generated method stub  
        return HibernateSessionFactory.getSession();
    }

    /**
     * 得到hql匹配的结果数，不要有select之类的
     *
     * @param hql
     * @return
     */
    public int getTotalCount(String hql) {
        // TODO Auto-generated method stub

        int result = 0;
        Session session = null;
        try {
            String strsql = this.getQueryTotalCountString(hql);
            session = this.getSession();
            Query query = session.createQuery(strsql);
            List list = query.list();
            result = this.getNum(list);
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /**
     * 通过hql语句和参数数组得到匹配总数
     *
     * @param hql
     * @param obj
     * @return
     */
    public int getTotalCounts(String hql, Object... obj) {
        // TODO Auto-generated method stub

        Session session = null;
        try {
            String strsql = this.getQueryTotalCountString(hql);
            //System.out.println(strsql);

            session = this.getSession();
            Query query = session.createQuery(strsql);
            for (int i = 0; i < obj.length; i++) {
                query.setParameter(i, obj[i]);
            }
            return getNum(query.list());
        } catch (Exception e) {
            String error = Messages.getString("BaseDao.getTotalCount.Error");

            e.printStackTrace();
            throw new DaoException(error);
        } finally {
            this.closeSession(session);
        }

    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#getTotalCount(java.lang.String, java.lang.Object) 
     */
    public int getTotalCountByObject(String hql, Object obj) {
        // TODO Auto-generated method stub  

        int result = 0;
        Session session = null;
        try {
            String strsql = this.getQueryTotalCountString(hql);
            //System.out.println(strsql);  

            session = this.getSession();
            Query query = session.createQuery(strsql);
            List list = query.setProperties(obj).list();
            result = this.getNum(list);

        } catch (Exception e) {
            String error = Messages.getString("BaseDao.getTotalCount.Error");

            e.printStackTrace();
            throw new DaoException(error);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#getTotalCount(java.lang.String, java.util.List) 
     */
    public int getTotalCountByListParams(String hql, List params) {
        // TODO Auto-generated method stub  
        String paramnameArray = "";
        if (params != null) {
            for (int i = 0; i < params.size(); i++) {
                if (i > 0)
                    paramnameArray += " , " + params.get(i);
                else
                    paramnameArray = "" + params.get(i);
            }
        }


        int result = 0;
        Session session = null;
        session = this.getSession();
        try {
            String strquery = this.getQueryTotalCountString(hql);
            //System.out.println(strsql);

            Query query = session.createQuery(strquery);
            if (params != null) {
                for (int i = 0; i < params.size(); i++) {
                    query.setParameter(i, params.get(i));
                }
            }
            List list = query.list();
            result = this.getNum(list);

        } catch (Exception e) {
            String error = Messages.getString("BaseDao.getTotalCount.Error");

            e.printStackTrace();
            throw new DaoException(error);
        } finally {
            this.closeSession(session);
        }
        return result;
    }


    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#getTotalCountBySql(java.lang.String) 
     */
    public int getTotalCountBySql(String strsql) {
        // TODO Auto-generated method stub  

        int result = 0;
        Session session = null;
        try {
            strsql = this.getQueryTotalCountString(strsql);
            session = this.getSession();

            List list = session.createSQLQuery(strsql).list();
            result = this.getNum(list);
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#getTotalCountBySql(java.lang.String, java.util.List) 
     */
    public int getTotalCountBySql(String strsql, List params) {
        // TODO Auto-generated method stub  
        String paramnameArray = "";
        if (params != null) {
            for (int i = 0; i < params.size(); i++) {
                if (i > 0)
                    paramnameArray += " , " + params.get(i);
                else
                    paramnameArray = "" + params.get(i);
            }
        }


        int result = 0;
        Session session = null;
        try {
            strsql = this.getQueryTotalCountString(strsql);

            session = this.getSession();
            SQLQuery query = session.createSQLQuery(strsql);
            if (params != null) {
                for (int i = 0; i < params.size(); i++) {
                    query.setParameter(i, params.get(i));
                }
            }
            List list = query.list();
            result = this.getNum(list);

        } catch (Exception e) {
            String error = Messages.getString("BaseDao.getTotalCount.Error");

            e.printStackTrace();
            throw new DaoException(error);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#loadAll(java.lang.String) 
     */
    public List loadAll(String hql) {
        // TODO Auto-generated method stub  
        return this.find(hql);
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#loadByPk(java.lang.Class, java.lang.String, java.lang.Object) 
     */
    public Object loadByPk(Class clazz, String keyName, Object keyValue) {
        // TODO Auto-generated method stub  
        Object result = null;
        String query = "from " + clazz.getName() + "  where " + keyName + "=? ";

        Session session = null;
        try {
            session = this.getSession();
            result = session.createCriteria(clazz).add(
                    Restrictions.eq(keyName, keyValue)).list();

        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#query(int, int, java.lang.String)
     */
    public List query(int pageNo, int pageSize, String hql) {
        // TODO Auto-generated method stub  

        List result = null;
        Session session = null;
        try {
            session = this.getSession();
            Query query = session.createQuery(hql);
            //System.out.println(queryString);  
            if (pageNo > 0 && pageSize > 0) {
                query.setFirstResult((pageNo - 1) * pageSize);
                query.setMaxResults(pageSize);
            }
            result = query.list();
            //System.out.println()  
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#query(int, int, java.lang.String, java.lang.Object)
     */
    public List queryByObject(int pageNo, int pageSize, String hql, Object obj) {
        // TODO Auto-generated method stub  

        List result = null;
        Session session = null;
        try {
            session = this.getSession();
            Query query = session.createQuery(hql);
            query.setProperties(obj);
            if (pageNo > 0 && pageSize > 0) {
                query.setFirstResult((pageNo - 1) * pageSize);
                query.setMaxResults(pageSize);
            }
            result = query.list();
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#query(int, int, java.lang.String, java.util.List)
     */
    public List queryByParamList(int pageNo, int pageSize, String hql, List params) {
        // TODO Auto-generated method stub  

        String paramnameArray = "";
        if (params != null) {
            for (int i = 0; i < params.size(); i++) {
                if (i > 0)
                    paramnameArray += " , " + params.get(i);
                else
                    paramnameArray = "" + params.get(i);
            }
        }

        List result = null;
        Session session = null;
        try {
            session = this.getSession();
            Query query = session.createQuery(hql);
            if (params != null) {
                for (int i = 0; i < params.size(); i++) {
                    query.setParameter(i, params.get(i));
                }
            }
            if (pageNo > 0 && pageSize > 0) {
                query.setFirstResult((pageNo - 1) * pageSize);
                query.setMaxResults(pageSize);
            }
            result = query.list();
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    public List query(Query query) {
        if (query == null) {
            return new ArrayList();
        }
        return query.list();
    }


    public List query(String hql){
        Session session=getSession();
        Query query=session.createQuery(hql);
        List<Object> list = query.list();
        return list;
    }
    /* (non-Javadoc)
   * @see zhenjw.hibernate.dao.IBaseDao#query(int, int, java.lang.String, java.util.List)
   */
    public List queryByParamArr(int startNo, int pageSize, String hql, Object... params) {
        // TODO Auto-generated method stub

        String paramnameArray = "";
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                if (i > 0)
                    paramnameArray += " , " + params[i];
                else
                    paramnameArray = "" + params[i];
            }
        }

        List result = null;
        Session session = null;
        try {
            session = this.getSession();
            Query query = session.createQuery(hql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    query.setParameter(i, params[i]);
                }
            }
            if (startNo > 0 && pageSize > 0) {
                query.setFirstResult(startNo);
                query.setMaxResults(pageSize);
            }
            result = query.list();
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#queryBySql(int, int, java.lang.String) 
     */
    public List queryBySql(int pageNo, int pageSize, String strsql) {
        List result = null;
        Session session = null;
        try {
            session = this.getSession();
            SQLQuery query = session.createSQLQuery(strsql);
            if (pageNo > 0 && pageSize > 0) {
                query.setFirstResult((pageNo - 1) * pageSize);
                query.setMaxResults(pageSize);
            }
            result = query.list();
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see zhenjw.hibernate.dao.IBaseDao#queryBySql(int, int, java.lang.String, java.util.List) 
     */
    public List queryBySql(int pageNo, int pageSize, String strsql, List params) {
        // TODO Auto-generated method stub  

        String paramnameArray = "";
        if (params != null) {
            for (int i = 0; i < params.size(); i++) {
                if (i > 0)
                    paramnameArray += " , " + params.get(i);
                else
                    paramnameArray = "" + params.get(i);
            }
        }

        List result = null;
        Session session = null;
        try {
            session = this.getSession();
            SQLQuery query = session.createSQLQuery(strsql);
            if (params != null) {
                for (int i = 0; i < params.size(); i++) {
                    query.setParameter(i, params.get(i));
                }
            }
            if (pageNo > 0 && pageSize > 0) {
                query.setFirstResult((pageNo - 1) * pageSize);
                query.setMaxResults(pageSize);
            }
            result = query.list();
        } catch (Exception e) {

            throw new DaoException(databaseError);
        } finally {
            this.closeSession(session);
        }
        return result;
    }



    /**
     * 功能：关闭session
     *
     * @param session
     */
    protected void closeSession(Session session) {
        if (session != null && session.isOpen())
            session.close();
        session = null;
    }

    /**
     * 功能：得到查询记录总数的语句（包含sql与hql）
     *
     * @param queryString
     * @return
     */
    private String getQueryTotalCountString(String queryString) {
        int form_index = queryString.indexOf("from ");
        int orderby_index = queryString.indexOf(" order by ");
        if (form_index < 0) {
            throw new DaoException(Messages
                    .getString("BaseDao.getTotalCount.notFromkeyword"));
        }
        String strsql = " select count(*) ";
        if (orderby_index > -1) {
            strsql = strsql + queryString.substring(form_index, orderby_index);
        } else {
            strsql = strsql + queryString.substring(form_index);
        }
        return strsql;
    }

    /**
     * 功能：得到记录数的方法
     *
     * @param list
     * @return
     */
    protected int getNum(List list) {
        int result = 0;
        if (list != null || list.size() > 0)
            result = Integer.parseInt(list.get(0).toString());
        return result;
    }


}  